#相关性系数和协方差
library(ggplot2)
print(head(economics))
#计算相关性
print(cor(economics$pce,economics$psavert))
#x轴与均值的差值
xpart <- economics$pce - mean(economics$pce)
ypart <- economics$psavert -mean(economics$psavert)
nMinusOne <- (nrow(economics)-1)
#x 轴的标准差.就是方差的平方根
xSD <- sd(economics$pce)
ySD <- sd(economics$psavert)
cor_value <- sum(xpart * ypart) /(nMinusOne * xSD * ySD)
print(cor_value)
print(cor(economics[,c(2,4:6)]))
library(GGally)
GGally::ggpairs(economics[,c(2,4:6)])
library(reshape2)
library(scales)
econCor <- cor(economics[,c(2,4:6)])
econMelt <- melt(econCor,varnames = c("x","y"),value.name = "Correlation")
print(econMelt)
